package com.linzm.leetcode.primary.exercises1_20230114;

/**
 * @Author zimingl
 * @Date 2023/1/15 15:12
 * @Description: 在区间范围内统计奇数数目
 */
public class Demo17_1523 {
    /**
     * 输入：low = 8, high = 10
     * 输出：1
     * 解释：8 到 10 之间奇数数字为 [9] 。
     */
    public static void main(String[] args) {
        Demo17_1523 demo17_1523 = new Demo17_1523();
        int low = 0;
        int high = 5;
        int countOdds = demo17_1523.countOdds2(low, high);
        System.out.println(countOdds);
    }

    private int countOdds(int low, int high) {
        int countOdds = 0;
        if (low % 2 != 0) {
            countOdds++;
        }
        if (high % 2 != 0) {
            countOdds++;
        }
        if (high % 2 != 0 && ((high - low)) % 2 == 0) {
            countOdds--;
        }
        countOdds += ((high - low) / 2);
        return countOdds;
    }

    public int countOdds2(int low, int high) {
        return pre(high) - pre(low - 1);
    }

    public int pre(int x) {
        return (x + 1) >> 1;
    }
}
